package vader.redisMode;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yongxin
 * @Title:
 * @Package
 * @Description:
 * @date 2021/3/1120:48
 */
public class RedisTempleDemo {

    @Autowired
    private RedisTempleMode redisTempleMode;
    @Autowired
    private RedisUtil redisUtil;

    public Object testRedisTempleMode(){
        //优化点
        JSONObject userMerchantInfo = JSONObject.parseObject(String.valueOf(
                redisUtil.get("product_id")));

        //redis优化--HashMap的获取方式
        String hashMapBaseKey = "product_map_key";
        String fieldKey = "product_map_field_key";
        long expire = 20L;
        //优先从redis中获取查询的商品信息，获取不到从数据库中读取，并设置到redis中
        JSONObject hashMapByKeyAndField = redisTempleMode.findHashMapByKeyAndField(hashMapBaseKey, fieldKey, expire,
                new TypeReference<JSONObject>() {},
                new RedisLoadable<JSONObject>() {
                    @Override
                    public JSONObject load() {
                        JSONObject queryResult = new JSONObject();
                        //todo 直接查询数据库
                        queryResult.put("AAAA_key", "AAAA_value");
                        queryResult.put("BBBB_key", "BBBB_value");
                        queryResult.put("CCCC_key", "CCCC_value");
                        return queryResult;
                    }
                });

        return null;
    }
}
